// pages/xqbq/xqbq.js
var app = getApp();
var api = require('../../utils/interface.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bq: [{
        id: 0,
        name: "宝爸",
        dianji: false
      },
      {
        id: 1,
        name: "成长金",
        dianji: false
      },
      {
        id: 2,
        name: "东方不败大锅饭",
        dianji: false
      },
      {
        id: 3,
        name: "宝妈",
        dianji: false
      },
      {
        id: 4,
        name: "阿道夫",
        dianji: false
      },
      {
        id: 5,
        name: "阿道夫",
        dianji: false
      },
      {
        id: 6,
        name: "玩儿",
        dianji: false
      },
      {
        id: 7,
        name: "泰国人",
        dianji: false
      },
      {
        id: 8,
        name: "给他",
        dianji: false
      },

    ],
    hiddenmodalput: true,
    reason_input: "",
    tage: [],
    poster: 'http://pic.pimg.tw/pam86591/1408719752-3322564110_n.jpg',
    name: 'Sugar',
    author: 'Maroon 5'
  },
  xz: function(e) {
    var bq = this.data.tage.slice(0);
    console.log(bq)
    var tl = bq.length;
    var ta = e.target.id;
    var dj = e.currentTarget.id;
    for (var i = 0; i < tl; i++) {
      if (ta == i) {
        if (bq[i].IsChoice == true) {
          bq[i].IsChoice = false;
        } else {
          bq[i].IsChoice = true;
        }
      }
    }
    this.setData({
      tage: bq
    })
  },

  xz1: function(e) {
    var bq = this.data.bq.slice(0);
    console.log(bq)
    var tl = bq.length;
    var ta = e.target.id;
    var dj = e.currentTarget.id;
    for (var i = 0; i < tl; i++) {
      if (ta == i) {
        if (bq[i].dianji == true) {
          bq[i].dianji = false;
        } else {
          bq[i].dianji = true;
        }
      }
    }
    this.setData({
      bq: bq
    })
  },
  bindinput: function(e) {
    this.setData({
      reason_input: e.detail.value,
    });

  },
  //点击按钮痰喘指定的hiddenmodalput弹出框  
  modalinput: function() {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  //取消按钮  
  cancel: function() {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function() {
    var that = this;
    // var bq = that.data.bq;
    // var bqLength = that.data.bq.length;
    // var reason_input = that.data.reason_input;
    //   bq.push({
    //     id: bqLength,
    //     name: reason_input,
    //     dianji: true
    //   });
    //   this.setData({
    //     hiddenmodalput: true,
    //     bq: bq
    //   })
    // },

    var bq = that.data.tage;
    var bqLength = that.data.bq.length;
    var reason_input = that.data.reason_input;
    if (reason_input != '') {
      bq.push({
        TagName: reason_input,
        IsSystem: true,
        IsChoice: true
      });
      this.setData({
        hiddenmodalput: true,
        tage: bq
      })
    }
    this.setData({
      hiddenmodalput: true
    })
  },

  saveInput: function() {
    var interests = "";
    var that = this;
    for (var i = 0; i < that.data.tage.length; i++) {
      if (that.data.tage[i].IsChoice) {
        interests += that.data.tage[i].TagName + "|";
      }
    }
    if (interests.length <= 0) {
      app.WxAlert('请选择你的兴趣');
      return;
    }
    interests = interests.substring(0, interests.length - 1);
    app.Post(api.wxapp.edit_user_info, {
      interests: interests
    }, function(res) {
      if (res.code == 1) {
        app.ShowMsg("保存成功");
        wx.navigateBack({
          delta: 1
        });
      } else {
        app.WxAlert(res.msg);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.get_tage_all();

  },
  get_tage_all: function() {
    var that = this;
    app.Post(api.wxapp.get_tage_all, {}, function(res) {
      if (res.code == 1) {
        that.setData({
          tage: res.data.TagList
        });
      } else {
        app.WxAlert(res.msg);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})